import 'package:enjoy_plus_01/pages/home/index.dart';
import 'package:enjoy_plus_01/pages/mine/index.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({ super.key });

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
   // 记录当前索引
  int currentIndex = 0;

  // 底部TabBar数据
  List tabBarList = [
    {
      "title": "首页",
      "default": "tabs/home_default.png",
      "active": "tabs/home_active.png"
    },
    {
      "title": "我的",
      "default": "tabs/my_default.png",
      "active": "tabs/my_active.png"
    }
  ];

  // 将数据生成对应的UI底部结构
  List<BottomNavigationBarItem> barItemList (){
     List<BottomNavigationBarItem> items = [];
    for (var item in tabBarList) {
      items.add(
        BottomNavigationBarItem(
          icon: Image.asset(
            item['default'],
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            item['active'],
            width: 32,
            height: 32,
          ),
          label: item['title'],
        ),
      );
    }
    return items;
  } 
     
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: currentIndex,
          children: const [
            HomePage(),
            MinePage()
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        // 底部图表的集合
        items: barItemList(),
        // 当前激活的索引
        currentIndex: currentIndex,
        selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
        unselectedItemColor: Colors.black,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }
}